﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
//using Evaluant.Uss.Linq;
//using Evaluant.Uss.ObjectContext;

using nPress.Core.Interfaces;

namespace nPress.Core.Repositories
{
    public class EussRepository : IRepository
    {
        //protected static readonly ObjectService os = new ObjectService();
        //protected ObjectContext oc;
        //protected bool completed;

        //public EussRepository() : this(os.CurrentObjectContext, true) { }
        //public EussRepository(ObjectContext oc) : this(oc, true) { }
        //public EussRepository(bool beginTransaction) : this(os.CurrentObjectContext, beginTransaction) { }
        //public EussRepository(ObjectContext oc, bool beginTransaction)
        //{
        //    this.oc = oc;
        //    if (beginTransaction)
        //        oc.BeginTransaction();
        //}


        //#region Count

       

        //public int Count<T>(string query) where T : class
        //{
        //    object o = oc.LoadScalar(string.Format("count({0}{1})", typeof(T).FullName.Replace(".", ":"), query));
        //    return (int)o;
        //}

        //public int Count<T>(Expression<Func<T, bool>> expression) where T : class
        //{
        //    return oc.Cast<T>().Where(expression).Count();
        //}

        //#endregion

        //#region Add

        //public void Add<T>(T entity) where T : class
        //{
        //    oc.Serialize(entity);
        //}

        //public void AddRange<T>(IEnumerable<T> entities) where T : class
        //{
        //    oc.Serialize(entities);
        //}


        //#endregion

        //#region Remove

        //public void Remove<T>(T entity) where T : class
        //{
        //    oc.Delete(entity);
        //}

        //#endregion

        //#region Save

        //public void Save<T>(T entity) where T : class
        //{
        //    oc.Serialize(entity);
        //}

        //public void Save<T>(T entity, object id) where T : class
        //{
        //    oc.SerializeDetached(entity, id.ToString());
        //}

        //public void Save<T>(IEnumerable<T> entities) where T : class
        //{
        //    oc.Serialize(entities);
        //}

        //#endregion

        //public T FindOne<T>(object id) where T : class
        //{
        //    return oc.LoadWithId<T>(id.ToString());
        //}

        //public T FindOne<T>(Expression<Func<T, bool>> expression)
        //{
        //    return oc.Cast<T>().Where(expression).First();
        //}

        //public T FindOne<T>(string query) where T : class
        //{
        //    return FindOne<T>(query, null);
        //}

        //public T FindOne<T>(string query, string[] inferredRelationship) where T : class
        //{
        //    Query q = new Query(typeof(T), query);
        //    if (inferredRelationship != null && inferredRelationship.Length > 0)
        //        q.InferredRelationships.AddRange(inferredRelationship);

        //    List<T> list = oc.Load<T>(q);
        //    if (list.Count < 1)
        //        throw new Exception("Any element corresponds to the query");
        //    if (list.Count > 1)
        //        throw new Exception("Only one element must correspond to the query");
        //    return list[0];
        //}

        //public bool TryFindOne<T>(string query, out T entity) where T : class
        //{
        //    try
        //    {
        //        List<T> list = oc.Load<T>(query);
        //        if (list.Count > 0)
        //            entity = list[0];
        //        else
        //            entity = null;

        //        return list.Count > 0;
        //    }
        //    catch
        //    {
        //        entity = null;
        //        return false;
        //    }
        //}

        //public T TryFindOne<T>(string query) where T : class
        //{
        //    try
        //    {
        //        T entity = null;

        //        TryFindOne(query, out entity);
        //        return entity;
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //}

        //public bool TryFindOne<T>(object id, out T entity) where T : class
        //{
        //    try
        //    {
        //        entity = oc.LoadWithId<T>(id.ToString());
        //        return true;
        //    }
        //    catch
        //    {
        //        entity = null;
        //        return false;
        //    }
        //}

        //public bool TryFindOne<T>(Expression<Func<T, bool>> expression, out T entity) where T : class
        //{
        //    T t = oc.Cast<T>().Where(expression).First();
        //    entity = t;

        //    return t != null;
        //}

        //public IList<T> FindAll<T>() where T : class
        //{
        //    return FindAll<T>(string.Empty);
        //}

        //public IList<T> FindAll<T>(Expression<Func<T, bool>> expression) where T : class
        //{
        //    return oc.Cast<T>().Where(expression).ToList();
        //}

        //public IList<T> FindAll<T>(string query) where T : class
        //{
        //    return FindAll<T>(query, new string[] { });
        //}

        //public IList<T> FindAll<T>(string query, string[] orderBy) where T : class
        //{
        //    return FindAll<T>(query, orderBy, new string[] { });
        //}

        //public IList<T> FindAll<T>(string query, string[] orderBy, string[] inferredRelationship) where T : class
        //{
        //    Query q = new Query(typeof(T), query);
        //    if (orderBy.Length > 0)
        //        q.OrderBy.AddRange(orderBy);
        //    if (inferredRelationship.Length > 0)
        //        q.InferredRelationships.AddRange(inferredRelationship);
        //    return oc.Load<T>(q);
        //}

        //public IList<T> FindAll<T>(object[] ids) where T : class
        //{
        //    string[] idString = new string[ids.Length];
        //    for (int i = 0; i < ids.Length; i++)
        //        idString[i] = ids[i].ToString();
            
        //    return oc.LoadWithId<T>(idString);
        //}

        //public IQueryable<T> Find<T>() where T : class
        //{
        //    return oc.Cast<T>();
        //}

        //public IQueryable<T> Find<T>(Expression<Func<T, bool>> expression) where T : class
        //{
        //    return oc.Cast<T>().Where(expression);
        //}

        //public void CreateRelationShip(object entity, object child, string propertyName)
        //{
        //    try
        //    {
        //        oc.CreateRelationship(entity, child, propertyName);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("CreateRelationShip Exception", ex);
        //    }
        //}

        //public void RemoveRelationShip(object entity, object child, string propertyName)
        //{
        //    try
        //    {
        //        oc.RemoveRelationship(entity, child, propertyName);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("RemoveRelationShip Exception", ex);
        //    }
        //}

        //public void Complete()
        //{
        //    oc.CommitTransaction();
        //    completed = true;
        //}

        //public bool TryComplete()
        //{
        //    try
        //    {
        //        oc.CommitTransaction();
        //        completed = true;
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        //public void ClearContext()
        //{
        //    oc.Clear();
        //}

        //public T Detach<T>(T entity) where T : class
        //{
        //    return oc.Detach(entity);
        //}

        //public void InitializeRepository()
        //{
        //    oc.InitializeRepository();
        //}
        public void Dispose()
        {
        //     if (!completed)
        //    {
        //        oc.RollBackTransaction();
        //    }
        }

        public void SaveOrUpdate(object entity)
        {
            throw new NotImplementedException();
        }

        public void Save(object entity)
        {
            throw new NotImplementedException();
        }

        public void Update(object entity)
        {
            throw new NotImplementedException();
        }

        public void CreateRelationShip(object entity, object child, string propertyName)
        {
            throw new NotImplementedException();
        }

        public void RemoveRelationShip(object entity, object child, string propertyName)
        {
            throw new NotImplementedException();
        }

        public T FindOne<T>(object id) where T : class
        {
            throw new NotImplementedException();
        }

        public T FindOne<T>(Expression<Func<T, bool>> expression) where T : class
        {
            throw new NotImplementedException();
        }

        public bool TryFindOne<T>(object id, out T entity) where T : class
        {
            throw new NotImplementedException();
        }

        public bool TryFindOne<T>(Expression<Func<T, bool>> expression, out T entity) where T : class
        {
            throw new NotImplementedException();
        }

        public IList<T> FindAll<T>() where T : class
        {
            throw new NotImplementedException();
        }

        public IList<T> FindAll<T>(Expression<Func<T, bool>> expression) where T : class
        {
            throw new NotImplementedException();
        }

        public IList<T> FindAll<T>(object[] ids) where T : class
        {
            throw new NotImplementedException();
        }

        public IQueryable<T> Find<T>() where T : class
        {
            throw new NotImplementedException();
        }

        public IQueryable<T> Find<T>(Expression<Func<T, bool>> expression) where T : class
        {
            throw new NotImplementedException();
        }

        public void Delete(object entity)
        {
            throw new NotImplementedException();
        }

        public int Count<T>() where T : class
        {
            throw new NotImplementedException();
        }

        public int Count<T>(Expression<Func<T, bool>> expression) where T : class
        {
            throw new NotImplementedException();
        }

        public void BeginTransaction()
        {
            throw new NotImplementedException();
        }

        public void CommitTransaction()
        {
            throw new NotImplementedException();
        }

        public void RollbackTransaction()
        {
            throw new NotImplementedException();
        }

        public bool Open()
        {
            throw new NotImplementedException();
        }

        public void Close()
        {
            throw new NotImplementedException();
        }

        public void Dispose(bool isDisposing)
        {
            throw new NotImplementedException();
        }

        public void ClearContext()
        {
            throw new NotImplementedException();
        }

        public T Detach<T>(T entity) where T : class
        {
            throw new NotImplementedException();
        }

        public void InitializeRepository()
        {
            throw new NotImplementedException();
        }
    }
}
